﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Announcement.DAL.DataSetApplyTableAdapters;
using Announcement.Model;


namespace Announcement
{
    public partial class _Default : LoginPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //取得表单数据
            string name = txtName.Text;
            string legal = txtLegal.Text;
            string cardType = ddlCardType.SelectedItem.Value.ToString();
            string cardNum = txtCardNum.Text;
            string agents = txtAgents.Text;
            string phone = txtPhone.Text;
            string cardType2 = ddlCardType2.SelectedItem.Value.ToString();
            string cardNum2 = txtCardNum2.Text;
            string houseLocal = txtHouseLocal.Text;
            string houseNum = txtHouseNum.Text;
            decimal money = Convert.ToDecimal(txtMoney.Text);

            int applyType = -1;
            if (rblApplyType.SelectedIndex > -1)//如果单选列表已选中
            {
                applyType = Convert.ToInt32(rblApplyType.SelectedValue);
            }

            DateTime applyDate = DateTime.Parse(txtApplyDate.Text);
            bool isPay = cbIsPay.Checked;

            //保存到数据库
            var applyRow = new T_ApplyTableTableAdapter();
            //插入同时取得主键值
            //int id = Convert.ToInt32(applyRow.QueryMaxId() + 1);

            //取得最后一个公告编号
            int maxId;
            //当插入第一条记录时
            if (applyRow.QueryMaxId() != null)
            {
                maxId = Convert.ToInt32(applyRow.QueryMaxId());
            }
            else 
            {
                maxId = 0;
            }
            if (maxId > 99999)
            {
                //超出编号最大能保存条目
                ClientScript.RegisterClientScriptBlock(typeof(Page), "已存在", @"alert('" + "超出编号最大能保存条目，请联系系统管理员。" + "');", true);
                return;
            }
            //类别编号
            int typeNum = this.getAnnouncementType(applyType);
            //计算公告编号
            string contractCode = Convert.ToString(DateTime.Now.Year * 1000000 + typeNum * 100000 + maxId + 1);

            /*
            int id = Convert.ToInt32(applyRow.InsertQueryGetId(name, legal, cardType, cardNum, agents, phone,
                cardType2, cardNum2, houseLocal, houseNum, applyType, applyDate, isPay, money));
            if (id >= 99999)
            {
                //超出编号最大能保存条目
            }
            //类别编号
            int typeNum = this.getAnnouncementType(applyType);
            //计算公告编号
            string contractCode = Convert.ToString(DateTime.Now.Year * 1000000 + typeNum * 100000 + id);
            //更新计算好的公告编号到数据库
            applyRow.UpdateContractCode(contractCode, id);
            */

            //保存信息到实体类
            ApplyTable at = new ApplyTable();
            at.Applicant = name;
            at.Legal = legal;
            at.CardTpye = cardType;
            at.CardNum = cardNum;
            at.Agents = agents;
            at.Phone = phone;
            at.CardType2 = cardType2;
            at.CardNum2 = cardNum2;
            at.HouseLocated = houseLocal;
            at.HouseNum = houseNum;
            at.Money = money;
            at.ApplyType = applyType;
            at.ApplyDate = applyDate;
            at.IsPay = isPay;
            at.ContractCode = contractCode;

            //把该实体保存到Session
            Session["ApplyTable"] = at;

            this.redirectPage(applyType);
          
        }

        private void redirectPage(int applyType)
        {
            switch (applyType)
            {
                case ApplyVar.ApplyType.OWNER:
                    Server.Transfer("Lost.aspx");
                    break;
                case ApplyVar.ApplyType.TOTAL:
                    Server.Transfer("Lost.aspx");
                    break;
                case ApplyVar.ApplyType.HIS:
                    Server.Transfer("Lost.aspx");
                    break;
                case ApplyVar.ApplyType.NOTICE:
                    Server.Transfer("Lost.aspx");
                    break;
                case ApplyVar.ApplyType.MORTGAGE:
                    Server.Transfer("Lost.aspx");
                    break;
                case ApplyVar.ApplyType.INITIAL:
                    Server.Transfer("Lost.aspx");
                    break;
                case ApplyVar.ApplyType.DEMOLITION:
                    Server.Transfer("Logout.aspx");
                    break;
                case ApplyVar.ApplyType.LEGAL:
                    Server.Transfer("Logout.aspx");
                    break;
                case ApplyVar.ApplyType.DIVORCE:
                    Server.Transfer("Shift.aspx");
                    break;
                case ApplyVar.ApplyType.GARAGE:
                    Server.Transfer("Garage.aspx");
                    break;
                case ApplyVar.ApplyType.PATCH:
                    Server.Transfer("Repair.aspx");
                    break;
                case ApplyVar.ApplyType.GIFT:
                    Server.Transfer("GiftShift.aspx");
                    break;
                case ApplyVar.ApplyType.OTHER:

                    break;
                default: break;
            }
        }

        private int getAnnouncementType(int applyType)
        {
            switch (applyType)
            {
                case ApplyVar.ApplyType.OWNER:
                    return ApplyVar.AnnouncementType.LOST;
                case ApplyVar.ApplyType.TOTAL:
                    return ApplyVar.AnnouncementType.LOST;
                case ApplyVar.ApplyType.HIS:
                    return ApplyVar.AnnouncementType.LOST;
                case ApplyVar.ApplyType.NOTICE:
                    return ApplyVar.AnnouncementType.LOST;
                case ApplyVar.ApplyType.MORTGAGE:
                    return ApplyVar.AnnouncementType.LOST;
                case ApplyVar.ApplyType.INITIAL:
                    return ApplyVar.AnnouncementType.LOST;
                case ApplyVar.ApplyType.DEMOLITION:
                    return ApplyVar.AnnouncementType.LOGOUT;
                case ApplyVar.ApplyType.LEGAL:
                    return ApplyVar.AnnouncementType.LOGOUT;
                case ApplyVar.ApplyType.DIVORCE:
                    return ApplyVar.AnnouncementType.REGISTER;
                case ApplyVar.ApplyType.GARAGE:
                    return ApplyVar.AnnouncementType.REGISTER;
                case ApplyVar.ApplyType.PATCH:
                    return ApplyVar.AnnouncementType.REGISTER;
                case ApplyVar.ApplyType.GIFT:
                    return ApplyVar.AnnouncementType.REGISTER;
                case ApplyVar.ApplyType.OTHER:
                    return ApplyVar.AnnouncementType.OTHER;
                default:
                    return 0;
            }
        }

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            Session["UserName"] = null;
            Response.Redirect("~/Login.aspx");
        }
    }
}
